<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
scope 2
</h1>
<p></p>
<div id="question" class="tmd-base">
<p></p>
<div class="tmd-usual">
What does the following program print?
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

var f = func(x int) {}

func Bar() {
	f := func(x int) {
		if x &gt;= 0 {
			print(x)
			f(x - 1)
		}
	}
	f(2)
}

func Foo() {
	f = func(x int) {
		if x &gt;= 0 {
			print(x)
			f(x - 1)
		}
	}
	f(2)
}

func main() {
	Bar()
	print(" | ")
	Foo()
}
</code></pre>
</div>
<p></p>
<div class="tmd-usual">
Choices:
</div>
<p></p>
<input type="radio" id="choiceA" name="choice" class="choice">
<input type="radio" id="choiceB" name="choice" class="choice">
<input type="radio" id="choiceC" name="choice" class="choice">
<input type="radio" id="choiceD" name="choice" class="choice">

<div id="choices">

<ul style="list-style-type:none;">
<li><label for="choiceA">2 | 2</label></li>
<li><label for="choiceB">2 | 210</label></li>
<li><label for="choiceC">210 | 2</label></li>
<li><label for="choiceD">210 | 210</label></li>
</ul>

</div><p></p>
<div id="answer" class="tmd-base">
<div class="tmd-usual">
Answer: 2 | 210
</div>
<p></p>
<div class="tmd-usual">
Run it on <a href="https://go.dev/play/p/eOaoWXNUKJ2">Go play</a>.
</div>
<p></p>
<div class="tmd-usual">
Key points:
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
In the <code class="tmd-code-span">Bar</code> function, the second <code class="tmd-code-span">f</code> (the one in the call <code class="tmd-code-span">f(x - 1)</code>) is the package-level <code class="tmd-code-span">f</code>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
In the <code class="tmd-code-span">Foo</code> function, all three <code class="tmd-code-span">f</code> are the package-level <code class="tmd-code-span">f</code>.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
Please read <a href="https://go101.org/article/blocks-and-scopes.html">code blocks and identifier scopes</a> for more detailed explanations.
</div>
<p></p>
</div>
<p></p>
</div>
